package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     *
     * @param order
     */
    void insert(Orders order);

    /**
     * 根据订单号和用户id查询订单
     *
     * @param orderNumber
     */
    @Select("select id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status " +
            "from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    /**
     * 分页查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 条件查询
     *
     * @param orders
     * @return
     */
    List<Orders> select(Orders orders);

    /**
     * 查询超时订单
     * @param status
     * @param overTime
     * @return
     */
    @Select("select id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status " +
            "from orders where status = #{status} and order_time <= #{overTime}")
    List<Orders> selectTimeOut(Integer status, LocalDateTime overTime);

    /**
     * 根据状态统计数量
     * @param status
     * @return
     */
    Integer selectCountByStatus(Integer status);

    /**
     * 根据状态和日期统计数量
     * @param status
     * @param begin
     * @param end
     * @return
     */
    Integer countByStatusAndDateTime(Integer status, LocalDateTime begin, LocalDateTime end);

    /**
     * 根据状态和日期统计营业额
     * @param status
     * @param begin
     * @param end
     */
    Double sumAmountByStatusAndDateTime(Integer status, LocalDateTime begin, LocalDateTime end);
}

